#include <assert.h>
#include <stdlib.h>
#include <sys/timeb.h> /* ftime() */
#include <stdio.h>
#include "test_general.h"

bool initilize_uthread_in_each_test;
	/* global variable */


double aux_iterations_per_sec;
/* This should hold a value such that:
		simple_loop(aux_iterations_per_sec)
	would take exactly one second.
*/

void simple_loop(double iterations_num)
{
	while (iterations_num >= 0) {
		report_running_of_curr_thread();
		iterations_num -= 1.0;
	}
}


/* init_ofer_test_module: caliberates aux_iterations_per_sec*/
void init_ofer_test_module()
{
	double big_number = 5000000.0;
	double start = get_precise_time();
	simple_loop(big_number);
	double end = get_precise_time();

	//printf("E-S: %lf\n", end-start);

	assert (end > start + 0.0001);
	aux_iterations_per_sec = big_number / (end-start);
}


void some_long_operation(double seconds)
{
	simple_loop(seconds * aux_iterations_per_sec);
}

double get_precise_time() {
	struct timeb tmp;
	ftime(&tmp);

	double s = tmp.time;
	double ms = tmp.millitm;

	return s + 0.001 * ms;
}

bool thread_ok[NR_THREADS];

void init_individual_test()
{
	int i;
	for (i = 0; i<NR_THREADS; i++) {
		thread_ok[i] = false;
		last_running_times[i] = -100000.0;
	}
}

bool are_all_threads_ok(int threads_num)
{
	int i;
	for (i= 0; i<threads_num; i++) {
		if (!thread_ok[i]) {
			return false;
		}
	}
	return true;
}


double last_running_times[200];
/* global variable */



void report_running_of_curr_thread()
{
	int id = uthread_self();
	assert (id>=0 && id<200);
	last_running_times[id] = get_precise_time();
}

